const mongoose = require('mongoose');
const { prodMongod } = require('../config');
const Grid = require('gridfs-stream');
const logUtil = require('./log_util');



mongoose.Promise = global.Promise;
Grid.mongo = mongoose.mongo;

const db = mongoose.createConnection(prodMongod.uri, prodMongod.options);
let gridFs=null;
// let projectJson;
db.once('open', () => {
  logUtil.info('mongodb connection opened');
   gridFs = Grid(db);
  // projectJson = db.model('CopyProjectJson', projectJsonSchema);
});
db.on('error', (err) => {
  logUtil.error('mongodb connection error', err);
  process.exit();
});


// const projectJsonSchema = new mongoose.Schema({
//   _class: String,
//   projectData: String,
//   projectVersion: String,
//   projectDesignId: String,
//   userId: String
// }, { collection: 'project_json' });




module.exports = {
  db,
  // ProjectJson(){
  //   // console.info('gfs is ', gfs);
  //   return ProjectJson();
  // },
  gridFs: () => new Promise((res, rej) => {
    res(gridFs);
  })
};
